<template>
  <div class="uploadImg">
    <!-- 1：上传图片
         2: 图片预览
         3：图片删除
         4：上传图片进度条
    -->
    <el-upload
      class="upload"
      action="#"
      :http-request="httpRequest"
      :before-upload="beforeUpload"
      :show-file-list="false"
    >
      <img v-errorImg v-if="value" class="img" :src="value" alt="" />
      <i v-else class="el-icon-plus icon" />
    </el-upload>
    <!-- 预览icon -->
    <i v-if="value" class="el-icon-zoom-in big" @click="show = true" />
    <!-- 删除 -->
    <i v-if="value" class="el-icon-circle-close del" @click="delEvent" />
    <!-- 进度条 -->
    <el-progress
      v-if="showProgress"
      type="circle"
      :percentage="percentage"
      class="progress"
      :width="180"
    />
    <!-- 图片dialog大图片 -->
    <el-dialog :visible.sync="show" title="预览图片">
      <div class="show-img">
        <img v-errorimg :src="value" alt="" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID1y3LbnMmGd1HPNH2g8TgkePL4rZOG0b6',
  SecretKey: '63nUeeRPLmDlDUbfZOBvmqt9NZqy10vw'
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      // value: '',
      show: false,
      showProgress: false,
      percentage: 0
    }
  },
  methods: {
    beforeUpload (file) {
      //   限制大小
      const limit1M = file.size / 1024 / 1024 < 1
      if (!limit1M) {
        this.$message.error('请上传1mb以内的图片')
      }
      //   限制类型
      const typeArr = ['image/png', 'image/jpeg']
      const limitType = typeArr.includes(file.type)
      if (!limitType) {
        this.$message.error('请上传png与jpg图片')
      }
      return limit1M && limitType
    },
    httpRequest (res) {
      this.showProgress = true
      cos.uploadFile(
        {
          Bucket: 'xxx-1305249343' /* 填写自己的 bucket，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: res.file
            .name /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */,
          Body: res.file, // 上传文件对象
          SliceSize:
            1024 *
            1024 *
            0.5 /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */,
          onProgress: progressData => {
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              this.showProgress = false
            }
          }
        },
        (err, data) => {
          if (err) {
            console.log('上传失败', err)
          } else {
            // this.value = 'http://' + data.Location
            this.$emit('input', 'http://' + data.Location)
            console.log('上传成功', data)
          }
        }
      )
    },
    // 删除事件
    delEvent () {
      this.$emit('input', '')
      // this.value = ''
    }
  }
}
</script>
<style lang="scss" scoped>
.upload {
  width: 180px;
  height: 180px;
  border: 1px dashed #000;
  display: flex;
  justify-content: center;
  align-items: center;
  &:hover {
    border-color: #409eff;
  }
  .icon {
    font-size: 30px;
  }
  .img {
    width: 180px;
    height: 180px;
    // contain:完整显示图片且保持比例
    // cover:填充所有区域且保持比例，图片可能有裁切
    // object-fit: cover;
    object-fit: contain;
  }
}
.uploadImg {
  position: relative;
  width: 180px;
  &:hover {
    .big,
    .del {
      display: block;
    }
  }
}
.big {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 30px;
  display: none;
}
.del {
  position: absolute;
  top: 10px;
  right: 45px;
  font-size: 30px;
  display: none;
}
.show-img {
  text-align: center;
  img {
    width: 100%;
  }
}

.progress {
  position: absolute;
  left: 0;
  top: 0;
  background-color: #fff;
}
</style>
